

const { ccclass, property } = cc._decorator;

@ccclass
export default class updateViewSize extends cc.Component {

    //-----------------------------------------------------
    //该脚本一般只在竖屏App上使用
    //-----------------------------------------------------



    // LIFE-CYCLE CALLBACKS:
    // private updateSizeEventIndex: number = null;

    onLoad() {
        // this.getStatusBarHeight();
        // this.getVirtualButtonHeight();
    }
    onEnable() {
        // this.updateSizeEventIndex = cc.vv.eventCustom.on('updateScreenSize', this.updateScreenSize.bind(this), this.node);
        // this.updateWidget();
    }
    onDisable() {
        // cc.vv.eventCustom.off('updateScreenSize', this.updateSizeEventIndex);
    }

    start() {
    }

    update(dt) { }
    private getStatusBarHeight() {
        let height = 0;//获取手机状态栏高度
        return height;
    }
    private getVirtualButtonHeight() {
        let height = 0;//获取下方虚拟键盘高度
        return height;
    }

    private getWidget() {
        let widget = this.node.getComponent(cc.Widget);
        if (!widget) {
            widget = this.node.addComponent(cc.Widget);
        }
        widget.enabled = true;
        return widget;
    }
    private updateWidget() {
        let widget = this.getWidget();
        widget.top = this.getStatusBarHeight();
        widget.bottom = this.getVirtualButtonHeight();
        widget.updateAlignment();
    }
    private updateScreenSize() {
        this.updateWidget();
    }
    /**
     * 后根更新layout
     * @param node 
     */
    public static UpdateLayout(node: cc.Node, isRecursive: boolean = true) {
        if (!node) return false;
        //先 更新子节点
        let layout: cc.Layout = null;
        if (isRecursive) {
            for (let i = 0; i < node.childrenCount; ++i) {
                let child = node.children[i];
                this.UpdateLayout(child, isRecursive);
                layout = child.getComponent(cc.Layout);
                layout && layout.updateLayout();
            }
        }
        layout = node.getComponent(cc.Layout);
        layout && layout.updateLayout();
        return true;
    }
    /**
     * 先根更新widget
     * @param node 
     */
    public static UpdateWidget(node: cc.Node, isRecursive: boolean = true) {
        if (!node) return false;
        //先 更新父节点
        let widget: cc.Widget = null;
        widget = node.getComponent(cc.Widget);
        widget && widget.updateAlignment();
        if (isRecursive) {
            for (let i = 0; i < node.childrenCount; ++i) {
                let child = node.children[i];
                this.UpdateWidget(child, isRecursive);
            }
        }
        return true;
    }
}
